package com.lsf.dunlian.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lsf.dunlian.R;
import com.lsf.dunlian.entity.ChatRecordEntity;
import com.lsf.dunlian.entity.GroupChatMessageVo;
import com.lsf.dunlian.enums.MsgTypeEnum;

import java.io.IOException;
import java.util.List;

public class GroupChatRecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ChatRecordEntity> mList;
    private Activity mContext;
    private LayoutInflater mInflater;
//    private MediaPlayer mediaPlayer;

    public interface OnItemClickListener {
        void onItemClick(GroupChatMessageVo bean);
    }

    public GroupChatRecycleViewAdapter(Activity context, List<ChatRecordEntity> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == ITEM_TYPE.TYPE_FRIEND_MSG.ordinal()) {
            return new FromMsgHolder(mInflater.inflate(R.layout.item_chat_friend, viewGroup, false));
        } else {
            return new ToMsgHolder(mInflater.inflate(R.layout.item_chat_me, viewGroup, false));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ChatRecordEntity chatWithFriendBean = mList.get(i);
        int itemViewType = getItemViewType(i);
        MediaPlayer mediaPlayer = null;
        if (itemViewType == ITEM_TYPE.TYPE_FRIEND_MSG.ordinal()) {
            Glide.with(mContext).load(chatWithFriendBean.receiveUserIcon).into(((FromMsgHolder) viewHolder).headImage);
            ((FromMsgHolder) viewHolder).name.setText(chatWithFriendBean.receiveUserName);

            if (MsgTypeEnum.MESSAGE_TEXT.getName().equals(chatWithFriendBean.msgType)) {
                ((FromMsgHolder) viewHolder).content.setVisibility(View.VISIBLE);
                ((FromMsgHolder) viewHolder).content.setTextIsSelectable(true);
                ((FromMsgHolder) viewHolder).audio_linear.setVisibility(View.GONE);
                ((FromMsgHolder) viewHolder).img.setVisibility(View.GONE);
                ((FromMsgHolder) viewHolder).content.setText(chatWithFriendBean.msgContent);
            }
            if (MsgTypeEnum.MESSAGE_IMG.getName().equals(chatWithFriendBean.msgType)) {
                ((FromMsgHolder) viewHolder).img.setVisibility(View.VISIBLE);
                ((FromMsgHolder) viewHolder).audio_linear.setVisibility(View.GONE);
                ((FromMsgHolder) viewHolder).content.setVisibility(View.GONE);
                ((FromMsgHolder) viewHolder).img.setOnClickListener(v -> {
                    showBigImg(chatWithFriendBean.msgContent);
                });
                Glide.with(mContext).load(chatWithFriendBean.msgContent).into(((FromMsgHolder) viewHolder).img);
            }
            if (MsgTypeEnum.MESSAGE_AUDIO.getName().equals(chatWithFriendBean.msgType)) {
                ((FromMsgHolder) viewHolder).audio_linear.setVisibility(View.VISIBLE);
                ((FromMsgHolder) viewHolder).content.setVisibility(View.GONE);
                ((FromMsgHolder) viewHolder).img.setVisibility(View.GONE);

                try {
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(chatWithFriendBean.msgContent);
                    mediaPlayer.prepare();
                    ((FromMsgHolder) viewHolder).audio_tv.setText((mediaPlayer.getDuration()/1000)+"秒");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                MediaPlayer finalMediaPlayer1 = mediaPlayer;
                ((FromMsgHolder) viewHolder).audio_linear.setOnClickListener(v -> {
                        finalMediaPlayer1.start();
                });
            }

        } else {
            Glide.with(mContext).load(chatWithFriendBean.createUserIcon).into(((ToMsgHolder) viewHolder).headImage);
            ((ToMsgHolder) viewHolder).name.setText(chatWithFriendBean.createUserName);
            if (MsgTypeEnum.MESSAGE_TEXT.getName().equals(chatWithFriendBean.msgType)) {
                ((ToMsgHolder) viewHolder).content.setVisibility(View.VISIBLE);
                ((ToMsgHolder) viewHolder).content.setTextIsSelectable(true);
                ((ToMsgHolder) viewHolder).img.setVisibility(View.GONE);
                ((ToMsgHolder) viewHolder).audio_linear.setVisibility(View.GONE);
                ((ToMsgHolder) viewHolder).content.setText(chatWithFriendBean.msgContent);
            }
            if (MsgTypeEnum.MESSAGE_IMG.getName().equals(chatWithFriendBean.msgType)) {
                ((ToMsgHolder) viewHolder).img.setVisibility(View.VISIBLE);
                ((ToMsgHolder) viewHolder).img.setOnClickListener(v -> {
                    showBigImg(chatWithFriendBean.msgContent);
                });
                ((ToMsgHolder) viewHolder).audio_linear.setVisibility(View.GONE);
                ((ToMsgHolder) viewHolder).content.setVisibility(View.GONE);
                Glide.with(mContext).load(chatWithFriendBean.msgContent).into(((ToMsgHolder) viewHolder).img);
            }


            if (MsgTypeEnum.MESSAGE_AUDIO.getName().equals(chatWithFriendBean.msgType)) {
                ((ToMsgHolder) viewHolder).audio_linear.setVisibility(View.VISIBLE);
                ((ToMsgHolder) viewHolder).content.setVisibility(View.GONE);
                ((ToMsgHolder) viewHolder).img.setVisibility(View.GONE);
                try {
                    mediaPlayer = new MediaPlayer();
                    mediaPlayer.setDataSource(chatWithFriendBean.msgContent);
                    mediaPlayer.prepare();
                    ((ToMsgHolder) viewHolder).audio_tv.setText((mediaPlayer.getDuration()/1000)+"秒");
                } catch (IOException e) {
                    e.printStackTrace();
                }

                MediaPlayer finalMediaPlayer = mediaPlayer;
                ((ToMsgHolder) viewHolder).audio_linear.setOnClickListener(v -> {
                    finalMediaPlayer.start(); // 准备好了就播放
                });
            }

        }
    }

    private void showBigImg(String url) {
//        LayoutInflater inflater = LayoutInflater.from(mContext);
//        View imgEntryView = inflater.inflate(R.layout.dialog_photo_entry, null); // 加载自定义的布局文件
//        AlertDialog dialog = new AlertDialog.Builder(mContext).create();
//        dialog.setInverseBackgroundForced(false);
//        ImageView img = imgEntryView.findViewById(R.id.large_image);
//        Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(img);
//        dialog.setView(imgEntryView);
//        dialog.show();
//        imgEntryView.setOnClickListener(paramView -> dialog.cancel());
//        img.setOnClickListener(v -> dialog.cancel());
    }


    public enum ITEM_TYPE {TYPE_FRIEND_MSG, TYPE_ME_MSG}

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).isFriendMsg) {
            return ITEM_TYPE.TYPE_FRIEND_MSG.ordinal();
        } else {
            return ITEM_TYPE.TYPE_ME_MSG.ordinal();
        }
    }

    public void update(List<ChatRecordEntity> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) return 0;
        return mList.size();
    }

    public static class FromMsgHolder extends RecyclerView.ViewHolder {
        private ImageView headImage;
        private ImageView img;
        private TextView content;
        private TextView name;
        private LinearLayout audio_linear;
        private TextView audio_tv;

        public FromMsgHolder(View itemView) {
            super(itemView);
            headImage = itemView.findViewById(R.id.friend_avatar);
            img = itemView.findViewById(R.id.img);
            content = itemView.findViewById(R.id.friend_content);
            audio_tv = itemView.findViewById(R.id.audio_tv);
            audio_linear = itemView.findViewById(R.id.audio_linear);
            name = itemView.findViewById(R.id.name);
        }
    }

    public static class ToMsgHolder extends RecyclerView.ViewHolder {
        private ImageView headImage;
        private ImageView img;
        private TextView content;
        private TextView name;
        private LinearLayout audio_linear;
        private TextView audio_tv;

        public ToMsgHolder(View itemView) {
            super(itemView);
            headImage = itemView.findViewById(R.id.me_avatar);
            img = itemView.findViewById(R.id.img);
            content = itemView.findViewById(R.id.me_content);
            audio_tv = itemView.findViewById(R.id.audio_tv);
            audio_linear = itemView.findViewById(R.id.audio_linear);
            name = itemView.findViewById(R.id.name);
        }
    }
}
